package com.lnlyj.media.ui;

import android.app.Activity;
import android.content.Intent;

import com.lnlyj.media.Config;
import com.lnlyj.media.R;
import com.lnlyj.media.base.BaseActivity;
import com.lnlyj.media.data.load.QueryConfig;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import static com.lnlyj.media.Constants.EXTRA_CONFIG;
import static com.lnlyj.media.Constants.EXTRA_PREVIEW_POSITION;
import static com.lnlyj.media.Constants.EXTRA_QUERY_CONFIG;
import static com.lnlyj.media.Constants.REQUEST_CODE_MEDIA;

/**
 * wanglei 2020-04-20
 */
public class AlbumActivity extends BaseActivity {


    private QueryConfig queryConfig;
    private Config config;
    private int position;

    private AlbumFragment fragment;

    public static void call(Activity activity, QueryConfig queryConfig, Config config, int position) {
        Intent intent = new Intent(activity, AlbumActivity.class);
        intent.putExtra(EXTRA_QUERY_CONFIG, queryConfig);
        intent.putExtra(EXTRA_CONFIG, config);
        intent.putExtra(EXTRA_PREVIEW_POSITION, position);

        activity.startActivityForResult(intent, REQUEST_CODE_MEDIA);
    }

    public static void call(Fragment fragment, QueryConfig queryConfig, Config config, int position) {
        Intent intent = new Intent(fragment.getActivity(), AlbumActivity.class);
        intent.putExtra(EXTRA_QUERY_CONFIG, queryConfig);
        intent.putExtra(EXTRA_CONFIG, config);
        intent.putExtra(EXTRA_PREVIEW_POSITION, position);

        fragment.startActivityForResult(intent, REQUEST_CODE_MEDIA);
    }


    public AlbumActivity() {
        super(R.layout.activity_album);
    }

    @Override
    public void initView() {



        Intent intent = getIntent();
        queryConfig = intent.getParcelableExtra(EXTRA_QUERY_CONFIG);
        config = intent.getParcelableExtra(EXTRA_CONFIG);
        position = intent.getIntExtra(EXTRA_PREVIEW_POSITION, 0);

        FragmentManager fm = getSupportFragmentManager();

        fragment = (AlbumFragment) fm.findFragmentByTag(AlbumFragment.TAG);

        if (fragment != null) {
            fm.beginTransaction().remove(fragment).commit();
        }
        fragment = AlbumFragment.getInstance(queryConfig, config, position);
        fm.beginTransaction().add(R.id.layout_content, fragment, AlbumFragment.TAG).show(fragment).commit();
    }

    @Override
    public void initData() {

    }
}
